#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // this project config.h
      
#include "common.h"
#include "util.h"

#include <assert.h>
#include <sys/time.h>
#include <X11/X.h>
#include <X11/Xlib.h>

void
lr_to_vb(float left, float right, float *volume, float balance)
{
	assert((left >= 0.0) && (right >= 0.0));

	*volume = THISMAX(left, right);

	if (left > right) {
		*balance = -1.0 + right / left;
	} else if (right > left) {
		*balance = 1.0 - left / right;
	} else {
		*balance = 0.0;
	}
}

void
vb_to_lr(float volume, float balance, float *left, float *right)
{
	*left = volume * (1.0 - THISMAX(0.0, balance));
	*right = volume * (1.0 + THISMIN(0.0, balance));
}

double
get_current_time(void)
{
	struct timeval tv;
	double t;

	gettimeofday(&tv, NULL);
	t = (double) tv.tv_sec;
	t += (double) tv.tv_usec / 1.0e6;

	return t;
}

#ifdef X_H
unsigned long
get_color(Display *display, char *color_name)
{
	XWindowAttributes winattrs;
	XColor color;

	XGetWindowAttributes(
			display,
			RootWindow(display, DefaultScreen(display)),
			&winattrs);

	color.pixel = 0;

	XParseColor(display, winattrs.colormap, color_name, &color);

	color.flags = DoRed | DoGreen | DoBlue;

	XAllocColor(display, winattrs.colormap, &color);

	return color.pixel;
}
#endif
